package com.burpee.web;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.burpee.service.BoardItemsService;
import com.burpee.service.BoardService;
import com.burpee.service.CommentsService;
import com.burpee.ui.Board;
import com.burpee.ui.Boarditems;
import com.burpee.ui.Comments;
import com.burpee.ui.Member;
@Controller
@RequestMapping("/board.spring")
public class BoardController {
	
	@Autowired
	BoardService boardService;
//	
//	@Autowired
//	AddfileService addfileService;
//	
	@Autowired
	BoardItemsService boardItemsService;
	
	@Autowired
	CommentsService commentsService;
	
	@RequestMapping(params = "action=premodify")
	public ModelAndView premodify(HttpServletRequest request, HttpServletResponse response, Board board){
		
		board = boardService.view(board.getBoardNo());
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("board", board);
		modelAndView.setViewName("board/boardmodify");
		
		return modelAndView;		
		
	}
	
///////////////////		일반 게시판의 게시글 리스트
	@RequestMapping(params = "action=boarditemsList")
	public ModelAndView boardItemsList(HttpServletRequest request, HttpServletResponse respons, long boardNo, Integer currentPage){
		
//		GeneratePaging generatePaging;
		
		String searchCondition = request.getParameter("searchCondition");
		String searchTitle = request.getParameter("searchTitle");
		
		if(currentPage == null){
			currentPage = 1;
		}
		
		List<Boarditems> boarditemsList = boardItemsService.list(boardNo, searchCondition, searchTitle, currentPage);
		
//		String paging = generatePaging.paging(currentPage, totalCount, "board.spring", 15, ".parsingPage", "board.spring");
		
		Board board = boardService.view(boardNo);
		
		int totalCount = boardItemsService.countBoardItem(boardNo, searchCondition, searchTitle);
		
		GeneratePaging generatePaging = new GeneratePaging();
		String paging = generatePaging.boardpaging(currentPage, totalCount, boardNo);
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("board", board);
		modelAndView.addObject("paging",paging);
		modelAndView.addObject("boardItems", boarditemsList);
		modelAndView.setViewName("bbs/boarditemsList");
		return modelAndView;
		
	}
	
	@RequestMapping(params = "action=boarditemsView")
	public ModelAndView view(HttpServletRequest request, HttpServletResponse respons,Boarditems boarditems){
	
//		Boarditems boarditems = boardItemsService.countBoardItem(boardListNo, searchCondition, searchTitle);
		
		boarditems = boardItemsService.view(boarditems.getBoarditemsNo());
		
		Board board = new Board();
		board = boardService.view(boarditems.getBoardNo());
		
		
		ModelAndView modelAndView = new ModelAndView();
		
		modelAndView.addObject("board",board);
		modelAndView.addObject("boarditems", boarditems);
		modelAndView.setViewName("bbs/boarditemsView");
		return modelAndView;
		
	}
	
	@RequestMapping(params = "action=boarditemsImagelist")
	public ModelAndView imagelist(HttpServletRequest request, HttpServletResponse respons){
		//////
		List<Boarditems> boarditemsList = boardItemsService.list(0, null, null, 0);
		
		ModelAndView modelAndView = new ModelAndView();
//		modelAndView.addObject("boardItems", boarditemsList);
		modelAndView.setViewName("bbs/albumBBS");
		
		return modelAndView;
		
	}
	
	@RequestMapping(params = "action=boardItemsPremodify")
	public ModelAndView boardItemsPremodify(long boardItemsNo){

		Boarditems boarditems=boardItemsService.view(boardItemsNo);

		ModelAndView modelAndView = new ModelAndView();

		modelAndView.addObject("boarditems",boarditems);

		modelAndView.setViewName("bbs/boarditemsModify");

		return modelAndView;

	}
	@RequestMapping(params="action=boardItemsModify")
	public ModelAndView boardItemsModify(Boarditems boarditems)
	{
		boarditems.setUpdateDate(new Timestamp(System.currentTimeMillis()));

		if (boarditems.getBoarditemsRemoveYn()==null) {

			boarditems.setBoarditemsNoticeYn("N");

		} else {

			boarditems.setBoarditemsNoticeYn("Y");

		}

		boolean result=boardItemsService.modify(boarditems);

		ModelAndView modelAndView =new ModelAndView();

		if (result) {

			boarditems=boardItemsService.view(boarditems.getBoarditemsNo());

			modelAndView.addObject("boarditems",boarditems);

			modelAndView.setViewName("bbs/boarditemsModify");

		} else {

		}
		return modelAndView;

	}
	
//	@RequestMapping(params = "action=boarditemsPreModify")
//	public ModelAndView boarditemsPreModify(HttpServletRequest request, HttpServletResponse respons){
//		
//		ModelAndView modelAndView = new ModelAndView();
////		modelAndView.addObject("boardItems", boarditemsList);
//		modelAndView.setViewName("bbs/boarditemsModify");
//		
//		return modelAndView;
//		
//	}
//	@RequestMapping(params = "action=modify")
//	public ModelAndView modify(HttpServletRequest request, HttpServletResponse response, Board board){
//		
//		boardService.modify(board);
//		
//		
//		board = boardService.view(board.getBoardNo());
//		ModelAndView modelAndView = new ModelAndView();
//		modelAndView.addObject("board", board);
//		modelAndView.setViewName("redirect:main.spring");
//		return modelAndView;
//		
//	}
	
	@RequestMapping(params = "action=boardItemsPreAdd")
	public ModelAndView boarditemsPreAdd(HttpServletRequest request, HttpServletResponse respons, Board board){

		
		board = boardService.view(board.getBoardNo());
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("board", board);
		modelAndView.setViewName("bbs/boarditemsAdd");

		return modelAndView;

	}


	@RequestMapping(params = "action=boardItemsAdd")
	public ModelAndView boarditemsAdd(HttpServletRequest request, HttpServletResponse respons, Boarditems boarditems){

		Member member = (Member)request.getSession().getAttribute("login");

		boarditems.setMemberId(member.getMemberId());

		if(boarditems.getBoarditemsNoticeYn() == null){
			boarditems.setBoarditemsNoticeYn("N");
		}
		else{
			boarditems.setBoarditemsNoticeYn("Y");
		}
		
		boarditems.setBoarditemsViewCnt(0);
		boarditems.setBoarditemsRemoveYn("N");
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());


		boarditems.setUpdateDate(timestamp);
		boarditems.setInputDate(timestamp);

		boolean result = boardItemsService.add(boarditems);

		if(result){

			int currentPage = 0;

			String strCurrentPage = request.getParameter("currenPage");

			if(strCurrentPage != null){
				currentPage = Integer.parseInt(strCurrentPage);
			}

//			return boardItemsList(request, respons);
			return null;
		}
		else{

			ModelAndView modelAndView = new ModelAndView();
			//modelAndView.addObject("message", "입력에 실패하였습니다.");
			modelAndView.setViewName("redirect:main.spring");

			return modelAndView;
		}

	}

	private String generatePaging(
			long boardListNo,
			int currentPage, 
			int totalCount,
			String searchCondition,
			String searchTitle) {
		
		if (searchCondition == null) {
			searchCondition = "";
		}
		
		if (searchTitle == null) {
			searchTitle = "";
		}
				
		int pageCount = totalCount / 20;
		
		if (totalCount % 20 != 0) {
			pageCount++;
		}
		
		int currentPageBlock = currentPage / 10;
		
		int startPage = currentPageBlock * 10 + 1;
		int maxPage = pageCount;
		
		if (startPage + 10 < pageCount) {
			maxPage = startPage + 9;
		}
		
		String url = "action=itemlist" +
				"&boardListNo=" + boardListNo + 
				"&searchCondition=" + searchCondition +
				"&searchTitle=" + searchTitle;
		
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<a href=\"javascript:itemListPaging('" + url + "&currentPage=1')\">" + "[泥섏쓬]</a>");

		if (currentPageBlock < 1) {
			stringBuffer.append("[<<]");
		}
		else {
			stringBuffer.append("<a href=\"javascript:itemListPaging('" + url + "&currentPage=" + (currentPageBlock * 10 - 9) + "')\">" + "[<<]</a>");
		}
		
		for (int i = startPage; i <= maxPage; i++) {
			
			if (i == currentPage) {
				stringBuffer.append("&nbsp;<b><a href=\"javascript:itemListPaging('" + url + "&currentPage=" + i + "')\">" + i + "</a></b>&nbsp;");
			}
			else {
				stringBuffer.append("&nbsp;<a href=\"javascript:itemListPaging('" + url + "&currentPage=" + i + "')\">" + i + "</a>&nbsp;");
			}
			
		}
		
		if (maxPage == pageCount) {
			stringBuffer.append("[>>]");
		}
		else {
			stringBuffer.append("<a href=\"javascript:itemListPaging('" + url + "&currentPage=" + (currentPageBlock * 10 + 11) + "')\">" + "[>>]</a>");
		}
		stringBuffer.append("<a href=\"javascript:itemListPaging('" + url + "&currentPage=" + pageCount + "')\">" + "[��</a>");
		
		return stringBuffer.toString();
	}

	@RequestMapping(params = "action=commentsadd")
	public ModelAndView commentsadd(HttpServletRequest request, HttpServletResponse response, Comments comments){
		
		Date date = new Date(System.currentTimeMillis());
		
		comments.setInputDate(date);
		comments.setUpdateDate(date);
		
		commentsService.add(comments);
		
		List<Comments> commentList = commentsService.list(comments.getBoarditemsNo());
		
		
		ModelAndView modelAndView = new ModelAndView();
	
		modelAndView.addObject("commentList", commentList);
		modelAndView.setViewName("bbs/commentlist");
		
		return modelAndView;
		
	}
	
	@RequestMapping (params = "action=commentspremodify")
	public ModelAndView replypremodify(Comments comments){
		comments = commentsService.view(comments);
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("comments", comments);
		modelAndView.setViewName("bbs/commentsmodify");
		
		return modelAndView;		
	}
	
	@RequestMapping (params = "action=commentsmodify")
	public ModelAndView replymodify(Comments comments){
		comments = commentsService.modify(comments);
		
		ModelAndView modelAndView = new ModelAndView();
		
		modelAndView.addObject("comments", comments);
		
		modelAndView.setViewName("bbs/commentsview");
		
		return modelAndView;		
	}
	
	@RequestMapping (params = "action=commentsremove")
	public ModelAndView replyremove(Comments comments){
		
		commentsService.remove(comments);
		
		
		List<Comments> commentsList = commentsService.list(comments.getBoarditemsNo());
		
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("commentsList", commentsList);
		
		modelAndView.setViewName("bbs/commentslist");
		
		return modelAndView;		
	}
	
	@RequestMapping (params = "action=commentsReplyAdd")
	public ModelAndView replyReplyAdd(Comments comments){
		
		comments.setInputDate(new Timestamp(System.currentTimeMillis()));
		comments.setUpdateDate(new Timestamp(System.currentTimeMillis()));
		
		
		
		commentsService.add(comments);
		
		List<Comments> commentList = commentsService.list(comments.getBoarditemsNo());		
		ModelAndView modelAndView = new ModelAndView();
	
		modelAndView.addObject("commentsList", commentList);
		
		modelAndView.setViewName("bbs/commentslist");
		
		return modelAndView;
		
	}

}
